package com.jzj.Dao;

import com.jzj.entity.Movie;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class Basedao {
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null;
    private static String Driver;
    private static String url;
    private static String username;
    private static String password;

    static {
        InputStream is = Basedao.class.getClassLoader().getResourceAsStream("com/jzj/Dao/a.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
            Driver=properties.getProperty("Driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void getConnect(){
        try {
            Class.forName(Driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void closeConnect(){
        try {
            resultSet.close();
            connection.close();
            preparedStatement.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public ResultSet selectSQL(String sql,String name){
        getConnect();
        if (name.equals("")){
            try {
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }finally {
                return resultSet;
            }
        }
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            return resultSet;
        }

    }
    public void addSQL(Movie movie){
        getConnect();
        String sql="INSERT INTO movie_info(movie_name,actor,dirctor,movie_time,type_id)VALUES(?,?,?,?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,movie.getName());
            preparedStatement.setString(2,movie.getActor());
            preparedStatement.setString(3,movie.getDirctor());
            preparedStatement.setObject(4,movie.getTime());
            preparedStatement.setString(5,movie.getType_name());
            int i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }
}
